package com.ruoyi.industryfrontend.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industryfrontend.domain.ProjectBuilding;
import com.ruoyi.industryfrontend.domain.ProjectPark;
import com.ruoyi.industryfrontend.domain.SpaceIndustry;
import com.ruoyi.industryfrontend.mapper.*;
import com.ruoyi.industryfrontend.service.IProjectBuildingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 项目-楼宇办公/商用Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-08
 */
@Service
public class ProjectBuildingServiceImpl implements IProjectBuildingService
{
    @Autowired
    private ProjectBuildingMapper projectBuildingMapper;

    @Autowired
    private ProjectParkMapper projectParkMapper;

    @Autowired
    private SpaceIndustryMapper spaceIndustryMapper;

    @Autowired
    private DistrictMapper districtMapper;

    @Autowired
    private SpaceCollectMapper spaceCollectMapper;
    /**
     * 查询项目-楼宇办公/商用
     *
     * @param id 项目-楼宇办公/商用主键
     * @return 项目-楼宇办公/商用
     */
    @Override
    public ProjectBuilding selectProjectBuildingById(Long id)
    {
        return projectBuildingMapper.selectProjectBuildingById(id);
    }

    /**
     * 根据编号查询楼宇信息
     * @param spaceId 编号
     * @return 结果
     */
    @Override
    public Map<String, Object> selectProjectBuildingByNumber(String spaceId) {
        ProjectBuilding projectBuilding = projectBuildingMapper.selectProjectBuildingByNumber(spaceId);
        List<String> list = spaceCollectMapper.selectSpaceCollectByUserId(SecurityUtils.getUserId(),spaceId);
        if (list!=null&&list.size()!=0){
            projectBuilding.setCollectState(1L);
        }else {
            projectBuilding.setCollectState(0L);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("space",projectBuilding);
        if (projectBuilding.getParkId()!=null) {
            ProjectPark projectPark = projectParkMapper.selectProjectParkById(projectBuilding.getParkId());
            map.put("park",projectPark);
        }
        return map;
    }

    /**
     * 查询项目-楼宇办公/商用列表
     *
     * @param projectBuilding 项目-楼宇办公/商用
     * @return 项目-楼宇办公/商用
     */
    @Override
    public List<ProjectBuilding> selectProjectBuildingList(ProjectBuilding projectBuilding)
    {
        return projectBuildingMapper.selectProjectBuildingList(projectBuilding);
    }
    @Override
    public List<ProjectBuilding> selectProjectBuildingListz(ProjectBuilding projectBuilding)
    {
//        List<Long> districtIds = new ArrayList<Long>();
//        //城市
//        if (projectBuilding.getCityId()!=null){
//            districtIds.add(projectBuilding.getCityId());
//            projectBuilding.setCityIds(districtIds);
//        }else if (projectBuilding.getCityId()==null && projectBuilding.getProvinceId() != null){
//            //查询省份下的城市
//            districtIds.addAll(districtMapper.selectDistrictIdList(projectBuilding.getProvinceId()));
//            districtIds.add(projectBuilding.getProvinceId());
//            projectBuilding.setCityIds(districtIds);
//        }
        if (projectBuilding.getBuildSellPrice()!=null){
            String[] split = projectBuilding.getBuildSellPrice().split("~");
            projectBuilding.setBuildSellPriceLow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                projectBuilding.setBuildSellPriceHigh(Long.parseLong(split[1]));
            }
        }
        //租赁价格
        if(projectBuilding.getBuildLeasePrice()!=null){
            String[] split = projectBuilding.getBuildLeasePrice().split("~");
            projectBuilding.setBuildLeasePriceLow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                projectBuilding.setBuildLeasePriceHigh(Long.parseLong(split[1]));
            }
        }
        return projectBuildingMapper.selectProjectBuildingListz(projectBuilding);
    }

    /**
     * 新增项目-楼宇办公/商用
     *
     * @param projectBuilding 项目-楼宇办公/商用
     * @return 结果
     */
    @Override
    public int insertProjectBuilding(ProjectBuilding projectBuilding)
    {
        projectBuilding.setSupplyUserId(SecurityUtils.getUserId());//发布人
        projectBuilding.setCreateTime(DateUtils.getNowDate());//创建时间
        projectBuilding.setApprovalState(0L);//审核状态
        projectBuilding.setSerialNumber(IdUtils.fastSimpleUUID());//编号
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectBuilding, spaceIndustry);
        spaceIndustry.setCreateBy(SecurityUtils.getUsername());
        spaceIndustry.setCreateTime(projectBuilding.getCreateTime());
        spaceIndustry.setSpaceId(projectBuilding.getSerialNumber());
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        spaceIndustryMapper.insertSpaceIndustry(spaceIndustry);
        return projectBuildingMapper.insertProjectBuilding(projectBuilding);
    }

    private static void extracted(ProjectBuilding projectBuilding, SpaceIndustry spaceIndustry) {
        spaceIndustry.setBuildType(projectBuilding.getBuildType());
        spaceIndustry.setReleaseType(0L);//供给
        spaceIndustry.setApprovalState(projectBuilding.getApprovalState());
        spaceIndustry.setProvinceId(projectBuilding.getProvinceId());//省份
        spaceIndustry.setCityId(projectBuilding.getCityId());//城市
//        spaceIndustry.setAreaId();//区域
        spaceIndustry.setSupplyType(projectBuilding.getSupplyType());//服务类型
        spaceIndustry.setParkId(String.valueOf(projectBuilding.getParkId()));
        spaceIndustry.setHeating(projectBuilding.getBuildHeating());//供暖
        spaceIndustry.setRefrig(projectBuilding.getBuildRefrig());//制冷
        spaceIndustry.setPower(projectBuilding.getBuildPower());//电力
        spaceIndustry.setGas(projectBuilding.getBuildGas());//燃气
        spaceIndustry.setLeasePrice(projectBuilding.getBuildLeasePrice());//出售价格
        spaceIndustry.setSellPrice(projectBuilding.getBuildSellPrice());//租赁价格
        spaceIndustry.setCoverResource(projectBuilding.getCoverResource());//封面资源
        spaceIndustry.setSpaceName(projectBuilding.getBuildName());//园区名称
        spaceIndustry.setSpaceAllSize(projectBuilding.getBuildAllSize());//空间总面积
        spaceIndustry.setTop(projectBuilding.getTop());//置顶

    }

    /**
     * 修改项目-楼宇办公/商用
     *
     * @param projectBuilding 项目-楼宇办公/商用
     * @return 结果
     */
    @Override
    public int updateProjectBuilding(ProjectBuilding projectBuilding)
    {
        projectBuilding.setApprovalState(0L);//未审核
        projectBuilding.setUpdateTime(DateUtils.getNowDate());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectBuilding,spaceIndustry);
        spaceIndustry.setSpaceId(String.valueOf(projectBuilding.getId()));
        spaceIndustry.setUpdateBy(SecurityUtils.getUsername());
        spaceIndustry.setUpdateTime(projectBuilding.getUpdateTime());
        spaceIndustryMapper.updateSpaceIndustryByBuildingId(spaceIndustry);
        return projectBuildingMapper.updateProjectBuilding(projectBuilding);
    }

    /**
     * 批量删除项目-楼宇办公/商用
     *
     * @param ids 需要删除的项目-楼宇办公/商用主键
     * @return 结果
     */
    @Override
    public int deleteProjectBuildingByIds(Long[] ids)
    {
        return projectBuildingMapper.deleteProjectBuildingByIds(ids);
    }

    /**
     * 删除项目-楼宇办公/商用信息
     *
     * @param id 项目-楼宇办公/商用主键
     * @return 结果
     */
    @Override
    public int deleteProjectBuildingById(Long id)
    {
        spaceIndustryMapper.deleteSpaceIndustryByProjectParkId(id);
        return projectBuildingMapper.deleteProjectBuildingById(id);
    }

    @Override
    public int deleteProjectBuildingBySerialNumber(String spaceId) {
        spaceIndustryMapper.deleteSpaceIndustryBySerialNumber(spaceId);
        return projectBuildingMapper.deleteProjectBuildingBySerialNumber(spaceId);
    }
}
